package com.guigu.www.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.pojo.Gather;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.pojo.Pay;
import com.guigu.www.pojo.PayDetails;
import com.guigu.www.service.CellService;
import com.guigu.www.service.PayDetailsService;
import com.guigu.www.service.PayService;
import com.guigu.www.service.imp.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PayController {
    //出库表
    @Autowired
    PayService payService;
    //出库明细
    @Autowired
    PayDetailsService payDetailsService;
    @Autowired
    CellService cellService;
    @Autowired
    IdUtils idUtils;
    //添加 出库表 和明细表数据
    @RequestMapping("/addParByPayDetailds.action")
    public Map addParByPayDetailds(@RequestBody Pay pay){
        System.out.println(pay);
        //创建map
        Map map=new HashMap();
        String is=pay.getReason().substring(pay.getReason().length()-1,pay.getReason().length());
        pay.setReasonexact(is+"0019422101");
        pay.setPaidAmountSum("0");
        pay.setCheckTag("S001-0");
        pay.setPayTag("");
        pay.setPayId(idUtils.getid7(pay));
        //添加明细表数据
        boolean f=payService.save(pay);
        //赋值
        List<PayDetails> payDetails=pay.getPayDetails();
        for (PayDetails payDetail : payDetails) {
            payDetail.setParentId(pay.getId());
            payDetail.setPayTag("");
            payDetail.setPaidAmount("0");
        }
        //添加明细
        boolean f1=payDetailsService.addParByPayDetailds(payDetails);
        //判断
        map.put("msg","出库申请失败");
        if(f && f1){
            map.put("msg","出库申请成功，等待审核");
        }
        return map;
    }


    //查询所以出库申请等待审核的
    @RequestMapping("/queryByPayCheckTag.action")
    public PageVo<Pay> queryByPayCheckTag(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                          @RequestParam(value = "pagesize",defaultValue ="5") int pagesize){

        //创建pageVo
        PageVo<Pay> payPageVo=new PageVo<Pay>();
        //获取方法
        IPage<Pay> iPage=payService.queryByPayCheckTag(pageno,pagesize);

        //赋值
        payPageVo.setRows(iPage.getRecords());
        payPageVo.setTotal(iPage.getTotal());

        return payPageVo;
    }


    //根据当前出库的编号查询
    @RequestMapping("/queryByPayPayId.action")
    public Pay queryByPayPayId(String payId){

        return payService.queryByPayPayId(payId);
    }

    //修改当前出库产品的状态为 通过或者不通过
    @RequestMapping("/updatePayByChecktag.action")
    public Map updatePayByChecktag(@RequestBody Pay pay){
        //创建map
        Map map=new HashMap();
        //判断是否通过
        //通过
        if(pay.getRad()==0){
            pay.setCheckTag("S001-1");
        }
        if(pay.getRad()==1){
           pay.setCheckTag("S001-2");
        }
        //调用修改状态
        boolean f=payService.updatePayByChecktag(pay);
        //判断
        map.put("msg","操作失败");
        if(f){
            map.put("msg","操作成功");
        }
        return map;
    }



    //出库申请查询
    @RequestMapping("/queryByPayAll.action")
    public PageVo<Pay> queryByPayAll(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                     @RequestParam(value = "pagesize",defaultValue ="5") int pagesize,
                                     Pay pay){
        //创建pagevo
        PageVo<Pay> payPageVo=new PageVo<Pay>();
        //调用方法
       IPage<Pay> iPage= payService.queryByPayAll(pageno,pagesize,pay);
        payPageVo.setTotal(iPage.getTotal());
        payPageVo.setRows(iPage.getRecords());
        return payPageVo;
    }


    //根据出库编号查询详情
    @RequestMapping("/queryByPayPayIdson.action")
    public Pay queryByPayPayIdson(String payId){
        //调用方法
        return  payService.queryByPayPayIdson(payId);
    }

    //查询所以出库申请审核成功的出库产品
    @RequestMapping("/queryByPayChecktagtg.action")
    public PageVo<Pay> queryByPayChecktagtg(@RequestParam(value ="pageno",defaultValue = "1") int pageno,
                                            @RequestParam(value = "pagesize",defaultValue ="5") int pagesize){

        //调用方法
        IPage<Pay> iPage=payService.queryByPayChecktagtg(pageno,pagesize);

        //创建pagevo
        PageVo<Pay> payPageVo=new PageVo<Pay>();

        //赋值
        payPageVo.setRows(iPage.getRecords());
        payPageVo.setTotal(iPage.getTotal());
        return  payPageVo;
    }

    //修改当前出库状态
    @RequestMapping("/updatePayByPayId.action")
    public Map updatePayByPayId(String payId, HttpSession session){
        //创建map
        Map map=new HashMap();
        //创建对象
        Pay pay=new Pay();
        pay.setAttemperTime(new Date());
        pay.setAttemper((String) session.getAttribute("username"));
        pay.setPayId(payId);
        System.out.println(pay);
        //调用方法
        boolean f=payService.updatePayByPayId(pay);
        //判断
        map.put("msg","调度失败");
        if(f){
            map.put("msg","调度成功");
        }
        return map;
    }

    //查询所有入库调度成功的产品
    @RequestMapping("/queryPayByChecktagAll.action")
    public PageVo<Pay> queryPayByChecktagAll(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                             @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){

        //创建apgevo
        PageVo<Pay> payPageVo=new PageVo<Pay>();

        //调用方法
        IPage<Pay> iPage=payService.queryPayByChecktagAll(pageno,pagesize);
        //赋值
        payPageVo.setTotal(iPage.getTotal());
        payPageVo.setRows(iPage.getRecords());
        return payPageVo;
    }

    //根据当前出库单号查询  明细  库存
    @RequestMapping("/queryByPayCellPayDetails.action")
    public Pay queryByPayCellPayDetails(String payId){
        return payService.queryByPayCellPayDetails(payId);
    }

    //修改当前出库单 出库明细单的状态
    @RequestMapping("/updatoutpeyPaydetailsBystatus.action")
    public Map updatoutpeyPaydetailsBystatus(@RequestBody  Pay pay){
        System.out.println(pay);
        //创建map
        Map map=new HashMap();
        //修改明细表数据
        boolean f1=payDetailsService.updatoutpeyPaydetailsBystatus(pay.getPayDetails());
        //修改出库表
        boolean f=payService.updatoutpeyPaydetailsBystatus(pay);
        //判断
        map.put("msg","登记失败");
        if(f && f1){
            map.put("msg","登记成功,等待审核");
        }
        return map;
    }
    //查询所有等待审核的出库单
    @RequestMapping("/queryByPaycheckpending.action")
    public PageVo<Pay> queryByPaycheckpending(@RequestParam(value = "pageno",defaultValue = "1") int pageno,@RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        //创建pageno
        PageVo<Pay> payPageVo=new PageVo<Pay>();
        //调用方法
        IPage<Pay> iPage=payService.queryByPaycheckpending(pageno,pagesize);
        //赋值
        payPageVo.setRows(iPage.getRecords());
        payPageVo.setTotal(iPage.getTotal());
        return payPageVo;
    }

    //修改当前状态  修改库存数量
    @RequestMapping("/updatePayPayDetailsCellstatus.action")
    public Map updatePayPayDetailsCellstatus(@RequestBody Pay pay){
        //创建map
        Map map=new HashMap();
        //修改当前库存当前的库存
        boolean f=payService.updateCellstatusaumont(pay.getPayDetails());
        boolean f1=payService.updatePayPayDetailsCellstatus(pay);
        //判断
        map.put("msg","审核失败");
        if(f && f1){
            map.put("msg","审核成功");
        }
        return map;
    }

    //登记查询查询所有调度完成的出库产品
    @RequestMapping("/outPayqllallshow.action")
    public PageVo<Pay> outPayqllallshow(@RequestParam(value = "pageno",defaultValue = "1")int pageno,
                                        @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){

        //创建page
        PageVo<Pay> pageVo=new PageVo<Pay>();

        //调用方法
        IPage<Pay> iPage=payService.outPayqllallshow(pageno,pagesize);
        //赋值
        pageVo.setRows(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        return pageVo;
    }

}
